<?php

namespace app\model;

use app\model\CourierInfo as ModelCourierInfo;
use app\model\DriverInfo as ModelDriverInfo;
use app\model\StationInfo as ModelStationInfo;
use app\model\UserOrder as ModelUserOrder;

use think\Db;

class CashStream extends Common
{
    protected static $obj;

    public function _initialize()
    {
        parent::_initialize();
        $this->db_name = 'cash_stream';
    }

    /**
     * 类入口
     * @return CashStream
     */
    public static function getInstance()
    {
        if (is_null(self::$obj))
            self::$obj = new self();
        return self::$obj;
    }
    /*
     * 添加流水
     */
    public function addCashStream($mapId,$mapType,$describe,$money,$balance,$isIncome,$orderNumber,$orderId)
    {
        $data['map_id'] = $mapId;
        $data['map_type'] = $mapType;
        $data['describe'] = $describe;
        $data['money'] = $money;
        $data['balance'] = $balance;
        $data['is_income'] = $isIncome;
        $data['order_number'] = $orderNumber;
        $data['order_id'] = $orderId;

        return $this->toAdd($data);

    }

    /*
     * 修改余额 --- 添加分佣
     * mapType ：1：快递员；2：司机；3站点
     * isIncome : 0 :收入；1：支出
     * money:金额、手续费
     */
    public function editBalance($mapType,$mapId,$isIncome,$money,$describe,$orderNumber,$orderId)
    {
        switch ($mapType){
            case 1 :
                $obj = ModelCourierInfo::getInstance();
                break;
            case 2 :
                $obj = ModelDriverInfo::getInstance();
                break;
            case 3 :
                $obj = ModelStationInfo::getInstance();
                break;
            default:
                _returnError(10014);
                break;
        }
        if ($isIncome == 0){ //收入
            $res = $obj->valueInc(['id'=>$mapId],'balance',$money);
            $balance = $obj->toValue(['id'=>$mapId],'balance');
            $streamRes = $this->addCashStream($mapId,$mapType,$describe,$money,$balance,$isIncome,$orderNumber,$orderId);

        }elseif ($isIncome == 1){ //支出
            $res = $obj->valueDec(['id'=>$mapId],'balance',$money);
            $balance = $obj->toValue(['id'=>$mapId],'balance');
            $streamRes = $this->addCashStream($mapId,$mapType,$describe,$money,$balance,$isIncome,$orderNumber,$orderId);
        }

        if ($res && $streamRes ) return true;
        return false;
    }


}
